#include "SceneObject.h"


SceneObject::SceneObject( const CCString& name, const CCString& layer )
	:m_name(name)
	,m_layer(layer)
	,m_destroy(false)
{

}

SceneObject::~SceneObject()
{
	ComponentList::iterator it = m_components.begin();
	for (; it != m_components.end(); it++ )
	{
		_deleteComponent(*it);
	}
}

Component* SceneObject::attachComponent( ComponentType type )
{
	Component* pComponent = _createComponent(type);
	CCAssert(pComponent != 0,"Count create this component!");
	if (pComponent)
	{
		m_components.push_back(pComponent);
	}
	return pComponent;
}

void SceneObject::detachComponent( Component* component )
{
	ComponentList::iterator it = m_components.begin();
	for (; it != m_components.end(); it++ )
	{
		if (component == *it )
		{
			_deleteComponent(component);
			m_components.erase(it);
			break;
		}
	}
}

void SceneObject::getComponents( ComponentType type, ComponentList& components )
{
	ComponentList::iterator it = m_components.begin();
	for (; it != m_components.end(); it++ )
	{
		if (type == (*it)->getType() )
		{
			components.push_back(*it);
		}
	}
}

Component* SceneObject::getComponentFromType( ComponentType type )
{
	ComponentList::iterator it = m_components.begin();
	for (; it != m_components.end(); it++ )
	{
		if (type == (*it)->getType() )
		{
			return *it;
		}
	}
	return 0;
}

void SceneObject::update( int deltaTimeMs )
{
	ComponentList::iterator it = m_components.begin();
	for (; it != m_components.end(); it++ )
	{
		(*it)->update(deltaTimeMs);
	}
}

Component* SceneObject::_createComponent( ComponentType type )
{
	switch (type)
	{
// 	case GAME_COMPONENT_MODEL: return new ModelComponent(this);
// 	case GAME_COMPONENT_PROPERTY:return new PropertyComponent(this);
// 	case GAME_COMPONENT_SKILL:return new SkillComponent(this);
// 	case GAME_COMPONENT_AI:return new AIComponent(this);
// 	case GAME_COMPONENT_EQUIPMENT:return new EquipmentComponent(this);
	}

	return 0;
}

void SceneObject::_deleteComponent( Component* pCom )
{
	switch (pCom->getType())
	{
// 	case GAME_COMPONENT_MODEL:
// 		delete static_cast<ModelComponent*>(pCom);
// 		break;
// 	case GAME_COMPONENT_PROPERTY:
// 		delete static_cast<PropertyComponent*>(pCom);
// 		break;
// 	case GAME_COMPONENT_SKILL:
// 		delete static_cast<SkillComponent*>(pCom);
// 		break;
// 	case GAME_COMPONENT_AI:
// 		delete static_cast<AIComponent*>(pCom);
// 		break;
// 	case GAME_COMPONENT_EQUIPMENT:
// 		delete static_cast<EquipmentComponent*>(pCom);
// 		break;
	}
}
